﻿using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;


public class Packager {
    public static string platform = string.Empty;
    static List<string> paths = new List<string>();
    static List<string> files = new List<string>();

    /// <summary>
    /// 载入素材
    /// </summary>
    static UnityEngine.Object LoadAsset(string file) {
        //if (file.EndsWith(".lua")) file += ".txt";
        return AssetDatabase.LoadMainAssetAtPath("Assets/Builds/" + file);
    }

    [MenuItem("Game/Build iPhone Resource", false, 11)]
    public static void BuildiPhoneResource() { 
        BuildTarget target;
        target = BuildTarget.iOS;
        BuildAssetResource(target, false);
    }

    [MenuItem("Game/Build Android Resource", false, 12)]
    public static void BuildAndroidResource() {
        BuildAssetResource(BuildTarget.Android, true);
    }

    [MenuItem("Game/Build Windows Resource", false, 13)]
    public static void BuildWindowsResource() {
        BuildAssetResource(BuildTarget.StandaloneWindows, true);
    }

    [MenuItem("Game/Build Windows 10 UWP Resource", false, 13)]
    public static void BuildWin10UwpResource()
    {
        BuildAssetResource(BuildTarget.WSAPlayer, true);
    }

    /// <summary>
    /// 生成绑定素材   重新写
    /// </summary>
    public static void BuildAssetResource(BuildTarget target, bool isWin)
    {
        BuildAssetBundleOptions options = BuildAssetBundleOptions.DeterministicAssetBundle |
                                         BuildAssetBundleOptions.UncompressedAssetBundle;
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, options, target);
 
        AssetDatabase.Refresh();
    }

    /// <summary>
    /// 数据目录
    /// </summary>
    static string AppDataPath {
        get { return Application.dataPath.ToLower(); }
    }

    [MenuItem("Game/Set Bundle Name")]
    public static void SetBundleName()
    {
        string currResFile = string.Empty;
        string bundleName = string.Empty;
        UnityEngine.Object[] selects = Selection.objects;
        for (int i = 0; i < selects.Length; i++)
        {
            if (selects[i].name.IndexOf('@') < 0)
            {
                currResFile = selects[i].name;
                bundleName = currResFile;
            }
            else if (currResFile == string.Empty || selects[i].name.IndexOf('@') > 0)
            {
                currResFile = selects[i].name.Split('@')[0];
                bundleName = currResFile + "_res";
            }
            
            string path = AssetDatabase.GetAssetPath(selects[i]);
            AssetImporter asset = AssetImporter.GetAtPath(path);

            string[] temp = asset.assetPath.Split('/');
            asset.assetBundleName = temp[temp.Length - 2] + "/" + bundleName; //设置Bundle文件的名称
            asset.assetBundleVariant = "";//设置Bundle文件的扩展名
            //asset.SaveAndReimport();
            UpdateProgress(i + 1, selects.Length, path);

        }
        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
    }

    public static void UpdateProgress(int progress, int progressMax, string desc)
    {
        string title = "Processing...[" + progress + " - " + progressMax + "]";
        float value = (float)progress / (float)progressMax;
        EditorUtility.DisplayProgressBar(title, desc, value);
    }
    /// <summary>
    /// 遍历目录及其子目录
    /// </summary>
    static void Recursive(string path) {
        string[] names = Directory.GetFiles(path);
        string[] dirs = Directory.GetDirectories(path);
        foreach (string filename in names) {
            string ext = Path.GetExtension(filename);
            if (ext.Equals(".meta")) continue;
            files.Add(filename.Replace('\\', '/'));
        }
        foreach (string dir in dirs) {
            paths.Add(dir.Replace('\\', '/'));
            Recursive(dir);
        }
    }
    static void BuildFileIndex()
    {
        //string resPath = AppDataPath + "/StreamingAssets/";
        /////----------------------创建文件列表-----------------------
        //string newFilePath = resPath + "/files.txt";
        //if (File.Exists(newFilePath)) File.Delete(newFilePath);

        //paths.Clear(); files.Clear();
        //Recursive(resPath);

        //FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
        //StreamWriter sw = new StreamWriter(fs);
        //for (int i = 0; i < files.Count; i++)
        //{
        //    string file = files[i];
        //    string ext = Path.GetExtension(file);
        //    if (file.EndsWith(".meta") || file.Contains(".DS_Store")) continue;

        //    string md5 = Util.md5file(file);
        //    string value = file.Replace(resPath, string.Empty);
        //    sw.WriteLine(value + "|" + md5);
        //}
        //sw.Close(); fs.Close();
    }
}